package admin

import (
	"chaowan/data_source"
	"chaowan/models"
	"github.com/gin-gonic/gin"
	"net/http"
)

func Niudantype(ctx *gin.Context) {

	var goodtype []models.Niudantype
	data_source.Db.Find(&goodtype)

	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    goodtype,
		"msg":     "ok",
	})
	return
}

type NiudantypeBody struct {
	ID   int64  `json:"id" gorm:"id"`
	Name string `json:"name" gorm:"name"`
}

func Niudantypeadd(ctx *gin.Context) {
	request := NiudantypeBody{}
	ctx.ShouldBind(&request)
	name := request.Name
	gt := models.Niudantype{
		Name: name,
	}
	data_source.Db.Create(&gt)
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    "ok",
		"msg":     "ok",
	})
	return
}
func Niudantypeupdate(ctx *gin.Context) {
	request := NiudantypeBody{}
	ctx.ShouldBind(&request)
	id := request.ID
	name := request.Name
	gt := models.Niudantype{
		Name: name,
	}
	data_source.Db.Where("id = ?", id).Updates(&gt)
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    "ok",
		"msg":     "ok",
	})
	return
}

func Niudantypedel(ctx *gin.Context) {
	request := NiudantypeBody{}
	ctx.ShouldBind(&request)
	id := request.ID

	data_source.Db.Where("id = ?", id).Delete(&models.Niudantype{})
	data_source.Db.Model(&models.Niudan{}).Where("id = ?", id).Update("status", 2)
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    "ok",
		"msg":     "ok",
	})
	return
}

type NiudansRBody struct {
	Data       []models.Niudan `json:"data"`
	PageNo     int             `json:"pageNo"`
	PageSize   int             `json:"pageSize"`
	TotalCount int64           `json:"totalCount"`
	TotalPage  int64           `json:"totalPage"`
}

type NiudansBody struct {
	Status       int    `json:"status"`
	Page         int    `json:"page"`
	Pagesize     int    `json:"pagesize"`
	Niudantypeid int64  `json:"niudantypeid" gorm:"niudantypeid"`
	Name         string `json:"name"`
	Paixu        int64  `json:"paixu"`
}

func Niudans(ctx *gin.Context) {
	request := NiudansBody{}
	ctx.ShouldBind(&request)
	status := request.Status
	niudantypeid := request.Niudantypeid
	name := request.Name
	currentPage := request.Page
	pageSize := request.Pagesize
	paixu := request.Paixu
	offsetNum := pageSize * (currentPage - 1)
	var count int64
	var users []models.Niudan
	res := data_source.Db
	if name != "" {
		res = res.Where("name like ?", "%"+name+"%")
	}
	if status > 0 {
		res = res.Where("status = ?", status)
	}
	if niudantypeid > 0 {
		res = res.Where("niudantypeid = ?", niudantypeid)
	}
	res = res.Model(&models.Niudan{}).Count(&count)
	res = res.Limit(pageSize).Offset(offsetNum)
	if paixu == 0 {
		res = res.Order("id desc").Find(&users)
	} else if paixu == 1 {
		res = res.Order("price asc").Find(&users)
	} else if paixu == 3 {
		res = res.Order("price desc").Find(&users)
	}
	var totalPage int64
	mo := count % int64(pageSize)
	if mo > 0 {
		totalPage = count/int64(pageSize) + 1
	} else {
		totalPage = count / int64(pageSize)
	}
	data := NiudansRBody{
		Data:       users,
		PageNo:     currentPage,
		PageSize:   pageSize,
		TotalCount: count,
		TotalPage:  totalPage,
	}

	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    data,
		"msg":     "ok",
	})
	return
}

type NiudanBody struct {
	ID           int64   `json:"id" gorm:"id"`
	Name         string  `json:"name" gorm:"name"`
	Niudantypeid int64   `json:"niudantypeid" gorm:"niudantypeid"`
	Image        string  `json:"image" gorm:"image"`
	Price        float64 `json:"price" gorm:"price"`         // 一发价格
	Jiazhi       string  `json:"jiazhi" gorm:"jiazhi"`       //价值
	Chuanshuo    float64 `json:"chuanshuo" gorm:"chuanshuo"` // 传说款比例
	Shishi       float64 `json:"shishi" gorm:"shishi"`       // 史诗款比例
	Putong       float64 `json:"putong" gorm:"putong"`       // 普通款比例
	Gaoji        float64 `json:"gaoji" gorm:"gaoji"`         // 高级款比例
	Fiveprice    float64 `json:"fiveprice" gorm:"fiveprice"` // 五发价格
	Tenprice     float64 `json:"tenprice" gorm:"tenprice"`   // 十发价格
	Status       int64   `json:"status" gorm:"status"`       // 1正常 2下架
	Images       string  `json:"images" gorm:"images"`
	Usernum      int64   `json:"usernum" gorm:"usernum"`
	Daynum       int64   `json:"daynum" gorm:"daynum"`
	Danchou      int64   `json:"danchou" gorm:"danchou"`
	Cjimage      string  `json:"cjimage" gorm:"cjimage"`
	Sort         int64   `json:"sort" gorm:"sort"`
}

func Niudansadd(ctx *gin.Context) {
	request := NiudanBody{}
	ctx.ShouldBind(&request)
	sort := request.Sort
	usernum := request.Usernum
	daynum := request.Daynum
	cjimage := request.Cjimage
	name := request.Name
	image := request.Image
	images := request.Images
	niudantypeid := request.Niudantypeid
	price := request.Price
	jiazhi := request.Jiazhi
	chuanshuo := request.Chuanshuo
	shishi := request.Shishi
	putong := request.Putong
	gaoji := request.Gaoji
	fiveprice := request.Fiveprice
	tenprice := request.Tenprice
	danchou := request.Danchou
	gt := models.Niudan{
		Usernum:      usernum,
		Danchou:      danchou,
		Daynum:       daynum,
		Name:         name,
		Image:        image,
		Images:       images,
		Niudantypeid: niudantypeid,
		Price:        price,
		Jiazhi:       jiazhi,
		Chuanshuo:    chuanshuo,
		Shishi:       shishi,
		Putong:       putong,
		Gaoji:        gaoji,
		Fiveprice:    fiveprice,
		Status:       2,
		Tenprice:     tenprice,
		Cjimage:      cjimage,
		Sort:         sort,
	}
	data_source.Db.Create(&gt)
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    "ok",
		"msg":     "ok",
	})
	return
}
func Niudansupdate(ctx *gin.Context) {
	request := NiudanBody{}
	ctx.ShouldBind(&request)
	id := request.ID
	sort := request.Sort
	usernum := request.Usernum
	daynum := request.Daynum
	name := request.Name
	image := request.Image
	images := request.Images
	niudantypeid := request.Niudantypeid
	price := request.Price
	jiazhi := request.Jiazhi
	chuanshuo := request.Chuanshuo
	shishi := request.Shishi
	putong := request.Putong
	gaoji := request.Gaoji
	tenprice := request.Tenprice
	fiveprice := request.Fiveprice
	danchou := request.Danchou
	cjimage := request.Cjimage
	//gt := models.Niudan{
	//	Usernum:      usernum,
	//	Daynum:       daynum,
	//	Name:         name,
	//	Image:        image,
	//	Images:       images,
	//	Niudantypeid: niudantypeid,
	//	Price:        price,
	//	Jiazhi:       jiazhi,
	//	Chuanshuo:    chuanshuo,
	//	Shishi:       shishi,
	//	Putong:       putong,
	//	Gaoji:        gaoji,
	//	Fiveprice:    fiveprice,
	//	Tenprice:     tenprice,
	//}
	//data_source.Db.Where("id = ?", id).Updates(&gt)
	data_source.Db.Model(&models.Niudan{}).Where("id = ?", id).Updates(map[string]interface{}{"usernum": usernum, "daynum": daynum, "name": name, "image": image, "images": images, "niudantypeid": niudantypeid, "price": price, "jiazhi": jiazhi, "chuanshuo": chuanshuo, "shishi": shishi, "putong": putong, "gaoji": gaoji, "fiveprice": fiveprice, "tenprice": tenprice, "danchou": danchou, "cjimage": cjimage, "sort": sort})

	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    "ok",
		"msg":     "ok",
	})
	return
}
func Niudansgrounding(ctx *gin.Context) {
	request := NiudanBody{}
	ctx.ShouldBind(&request)
	id := request.ID
	status := request.Status

	if status == 1 {
		var niudangood []models.Niudangood
		data_source.Db.Where("niudanid = ?", id).Find(&niudangood)
		if len(niudangood) == 0 {
			ctx.JSON(http.StatusOK, gin.H{
				"code":    400,
				"error":   1,
				"success": 400,
				"data":    "on",
				"msg":     "商品总数错误，无法上架",
			})
			return
		}

		var zhuanpan models.Zhuanpan
		data_source.Db.Where("state = 2").Where("fanshangid = ?", id).First(&zhuanpan)
		if zhuanpan.ID == 0 {
			ctx.JSON(http.StatusOK, gin.H{
				"code":    400,
				"error":   1,
				"success": 400,
				"data":    "on",
				"msg":     "未添加，无法上架",
			})
			return
		}

		var num int64
		var goodnum int64
		a := 1
		b := 1
		c := 1
		d := 1
		for _, v := range niudangood {
			if v.Classify == 1 {
				if a == 1 {
					num = num + 1
					a = 2
				}
			}
			if v.Classify == 2 {
				if b == 1 {
					num = num + 1
					b = 2
				}
			}
			if v.Classify == 3 {
				if c == 1 {
					num = num + 1
					c = 2
				}
			}
			if v.Classify == 4 {
				if d == 1 {
					num = num + 1
					d = 2
				}
			}
			goodnum = goodnum + v.Num
		}
		if goodnum == num*1000000 {
			data_source.Db.Model(&models.Niudan{}).Where("id = ?", id).Update("status", status)
		} else {
			ctx.JSON(http.StatusOK, gin.H{
				"code":    400,
				"error":   1,
				"success": 400,
				"data":    "on",
				"msg":     "商品总数错误，无法上架",
			})
			return
		}

	} else if status == 2 {
		data_source.Db.Model(&models.Niudan{}).Where("id = ?", id).Update("status", status)
	}

	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    "ok",
		"msg":     "ok",
	})
	return
}
func Niudaninfo(ctx *gin.Context) {
	request := NiudanBody{}
	ctx.ShouldBind(&request)
	id := request.ID
	var good models.Niudan
	data_source.Db.Where("id = ?", id).First(&good)
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    good,
		"msg":     "ok",
	})
	return
}

type NiudangoodsRBody struct {
	Data       []models.Niudangoodsql `json:"data"`
	PageNo     int                    `json:"pageNo"`
	PageSize   int                    `json:"pageSize"`
	TotalCount int64                  `json:"totalCount"`
	TotalPage  int64                  `json:"totalPage"`
}

type NiudangoodsBody struct {
	Page     int   `json:"page"`
	Pagesize int   `json:"pagesize"`
	Niudanid int64 `json:"niudanid" gorm:"niudanid"`
}

func Niudangoods(ctx *gin.Context) {
	request := NiudangoodsBody{}
	ctx.ShouldBind(&request)
	niudanid := request.Niudanid
	currentPage := request.Page
	pageSize := request.Pagesize
	offsetNum := pageSize * (currentPage - 1)
	var count int64
	var niudangoodsql []models.Niudangoodsql
	res := data_source.Db.Model(&models.Niudangood{}).Select("niudangood.*, goods.name, goods.image, goods.price").Joins("left join goods on goods.id = niudangood.goodid").Where("niudangood.niudanid = ?", niudanid)
	res.Model(&models.Niudangood{}).Count(&count)
	res.Limit(pageSize).Offset(offsetNum).Order("id desc").Find(&niudangoodsql)

	var totalPage int64
	mo := count % int64(pageSize)
	if mo > 0 {
		totalPage = count/int64(pageSize) + 1
	} else {
		totalPage = count / int64(pageSize)
	}
	data := NiudangoodsRBody{
		Data:       niudangoodsql,
		PageNo:     currentPage,
		PageSize:   pageSize,
		TotalCount: count,
		TotalPage:  totalPage,
	}

	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    data,
		"msg":     "ok",
	})
	return
}

type NiudangoodBody struct {
	ID       int64 `json:"id" gorm:"id"`
	Goodid   int64 `json:"goodid" gorm:"goodid"`
	Classify int64 `json:"classify" gorm:"classify"` // 1传说款 2史诗款 3普通款 4高级款
	Niudanid int64 `json:"niudanid" gorm:"niudanid"`
	Num      int64 `json:"num" gorm:"num"`
}

func Niudangoodadd(ctx *gin.Context) {
	request := NiudangoodBody{}
	ctx.ShouldBind(&request)
	goodid := request.Goodid
	classify := request.Classify
	niudanid := request.Niudanid
	num := request.Num
	gt := models.Niudangood{
		Goodid:   goodid,
		Classify: classify,
		Niudanid: niudanid,
		Num:      num,
	}
	data_source.Db.Create(&gt)
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    "ok",
		"msg":     "ok",
	})
	return
}
func Niudangoodupdate(ctx *gin.Context) {
	request := NiudangoodBody{}
	ctx.ShouldBind(&request)
	id := request.ID
	goodid := request.Goodid
	classify := request.Classify
	niudanid := request.Niudanid
	num := request.Num
	gt := models.Niudangood{
		Goodid:   goodid,
		Classify: classify,
		Niudanid: niudanid,
		Num:      num,
	}
	data_source.Db.Where("id = ?", id).Updates(&gt)
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    "ok",
		"msg":     "ok",
	})
	return
}
func Niudangooddel(ctx *gin.Context) {
	request := NiudangoodBody{}
	ctx.ShouldBind(&request)
	id := request.ID
	data_source.Db.Where("id = ?", id).Delete(&models.Niudangood{})
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    "ok",
		"msg":     "ok",
	})
	return
}
